package com.cheer.service.impl;

import cn.hutool.core.date.DateUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.cheer.common.Page;
import com.cheer.dao.MallComplaintDao;
import com.cheer.dto.MallComplaintAddDTO;
import com.cheer.dto.MallComplaintQueryDTO;
import com.cheer.dto.MallComplaintReplyDTO;
import com.cheer.entity.MallComplaintEntity;
import com.cheer.entity.OaUserEntity;
import com.cheer.enums.MallComplaintStatusEnum;
import com.cheer.exception.ServiceException;
import com.cheer.service.MallComplaintService;
import com.cheer.service.OaUserService;
import com.cheer.vo.MallComplaintListVO;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

@Service("mallComplaintService")
public class MallComplaintServiceImpl extends ServiceImpl<MallComplaintDao, MallComplaintEntity> implements MallComplaintService {

    @Autowired
    private MallComplaintDao mallComplaintDao;

    @Autowired
    private OaUserService oaUserService;

    @Override
    public Page<MallComplaintListVO> listByQueryCondition(MallComplaintQueryDTO complaintQueryDTO) {
        IPage<MallComplaintEntity> page = mallComplaintDao.selectListByQueryCondition(complaintQueryDTO);

        List<MallComplaintListVO> complaintList = page.getRecords().stream()
                .map(complaintEntity -> new MallComplaintListVO()
                        .setId(complaintEntity.getId())
                        .setCreateTime(DateUtil.formatDateTime(complaintEntity.getCreateTime()))
                        .setMemberName(complaintEntity.getRealName())
                        .setPhone(complaintEntity.getPhone())
                        .setOrderNumber(complaintEntity.getOrderNumber())
                        .setContent(complaintEntity.getContent())
                        .setReply(complaintEntity.getReply())
                        .setRemark(complaintEntity.getRemark())
                        .setStatus(complaintEntity.getStatus()))
                .collect(Collectors.toList());

        return new Page<>(page, complaintList);
    }

    @Override
    public void reply(MallComplaintReplyDTO complaintReplyDTO) {
        MallComplaintEntity complaint = mallComplaintDao.selectById(complaintReplyDTO.getId());
        if (complaint == null) {
            throw new ServiceException("投诉不存在");
        }

        MallComplaintEntity updateComplaint = new MallComplaintEntity()
                .setId(complaintReplyDTO.getId())
                .setReply(complaintReplyDTO.getReply())
                .setRemark(complaintReplyDTO.getRemark())
                .setStatus(MallComplaintStatusEnum.SOLVED.getCode());

        mallComplaintDao.updateById(updateComplaint);
    }

    @Override
    public void delete(List<Long> ids) {
        if (CollectionUtils.isEmpty(ids)) {
            return;
        }

        mallComplaintDao.deleteBatchIds(ids);
    }

    @Override
    public void add(MallComplaintAddDTO complaintAddDTO, Integer userId) {
        OaUserEntity user = oaUserService.getById(userId);
        if (user == null) {
            throw new ServiceException("用户不存在");
        }

        MallComplaintEntity complaint = new MallComplaintEntity()
                .setUserId(userId)
                .setRealName(user.getName())
                .setOrderNumber(complaintAddDTO.getOrderNumber())
                .setPhone(complaintAddDTO.getPhone())
                .setContent(complaintAddDTO.getContent())
                .setStatus(MallComplaintStatusEnum.NOT_SOLVED.getCode());

        mallComplaintDao.insert(complaint);
    }

    @Override
    public List<MallComplaintListVO> listByUserId(Integer userId) {
        List<MallComplaintEntity> complaintList = mallComplaintDao.selectListByUserId(userId);

        return complaintList.stream()
                .map(complaint -> new MallComplaintListVO()
                        .setId(complaint.getId())
                        .setContent(complaint.getContent())
                        .setStatus(complaint.getStatus()))
                .collect(Collectors.toList());
    }

    @Override
    public MallComplaintListVO info(Long id) {
        MallComplaintEntity complaint = mallComplaintDao.selectById(id);
        if(complaint==null){
            throw new ServiceException("投诉不存在");
        }

       return new MallComplaintListVO()
                .setId(complaint.getId())
                .setOrderNumber(complaint.getOrderNumber())
                .setContent(complaint.getContent())
                .setReply(complaint.getReply());
    }
}